package com.zch.house.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.house.dao.FlatPlayDao;
import com.zch.house.entity.FlatPlay;
import com.zch.house.service.FlatPlayService;
import com.zch.house.util.XmlSession;

public class FlatPlayServiceImpl extends XmlSession implements FlatPlayService {
	private final int PAGE_SIZE = 20;
	{
		
	}
	@Override
	public List<FlatPlay> getAllFlatPlayByPage(Date startDate, Date endDate, String topic, int pageIndex) {
		//使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("topic", topic);
		int firstRow = (pageIndex - 1)*PAGE_SIZE + 1;
		int lastRow = pageIndex * PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
		//调用dao层方法
		List<FlatPlay> list =  dao.getAllFlatPlay(map);
		//关闭提交事务并关闭session
		closeSession();
		return list;
	}

	@Override
	public int getTotalCount(Date startDate, Date endDate, String topic) {
		//使用map集合来存放参数
				Map<String, Object> map = new HashMap<>();
				map.put("startDate", startDate);
				map.put("endDate", endDate);
				map.put("topic", topic);
				//加载配置文件
				initSession();
				//获得mapper的代理类
				FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
				//调用dao层方法
				int counts =  dao.getTotalCount(map);
				//关闭提交事务并关闭session
				closeSession();
				//计算总页数
				int totalPages = counts%PAGE_SIZE==0?counts/PAGE_SIZE:counts/PAGE_SIZE + 1;
				return totalPages;
	}

	@Override
	public FlatPlay getFlatPlayById(int id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
		//调用dao层方法
		FlatPlay flatPlay =  dao.getFlatPlayById(id);
		//关闭提交事务并关闭session
		closeSession();
		return flatPlay;
	}

	@Override
	public boolean addFlatPlay(FlatPlay flatPlay) {
				//加载配置文件
				initSession();
				//获得mapper的代理类
				FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
				//调用dao层方法
				boolean flag = dao.addFlatPlay(flatPlay);
				//关闭提交事务并关闭session
				closeSession();
				return flag;
	}

	@Override
	public boolean deleteFlatPlay(int id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
		//调用dao层方法
		boolean flag = dao.deleteFlatPlay(id);
		//关闭提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean updateFlatPlay(FlatPlay flatPlay) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatPlayDao dao = session.getMapper(FlatPlayDao.class);
		//调用dao层方法
		boolean flag = dao.updateFlatPlay(flatPlay);
		//关闭提交事务并关闭session
		closeSession();
		return flag;
	}

}
